
import { generateProblems } from './problems';


export const verificationCodes = {
  'test@example.com': '123456',
  'user1@example.com': '123456',
  'user2@example.com': '123456',
  'user3@example.com': '123456',
  'user4@example.com': '123456'
};
export const avatar_male = 'http://oj-1325679006.cos.ap-beijing.myqcloud.com/5304dd02c0b35bc789ef47c3feee5a57';
export const avatar_female = 'http://oj-1325679006.cos.ap-beijing.myqcloud.com/00fcbcc621adcea447bb245a2b7e7408';
// 用户数据
export const users = [
  {
    email: 'test@example.com',
    username: '测试用户',
    password: '123123',
    avatar: avatar_male,
    useDefaultAvatar: true,
    createdAt: '2025-01-01T00:00:00Z',
    token: 'test@example.com',
    version: 'test@example.com',
    following: [
      'user1@example.com',
      'user2@example.com',
      'user3@example.com',
      'user4@example.com'
    ],
    followers: [
      'user1@example.com',
      'user2@example.com',
      'user3@example.com',
      'user4@example.com'
    ]
  },
  {
    email: 'user1@example.com',
    username: 'user1',
    password: '123123',
    avatar: avatar_female,
    useDefaultAvatar: true,
    createdAt: '2025-01-02T00:00:00Z',
    token: 'user1@example.com',
    version: 'user1@example.com',
    following: [
      'test@example.com',
      'user2@example.com',
      'user3@example.com'
    ],
    followers: [
      'test@example.com',
      'user2@example.com',
      'user4@example.com'
    ]
  },
  {
    email: 'user2@example.com',
    username: 'user2',
    password: '123123',
    avatar: avatar_male,
    useDefaultAvatar: true,
    createdAt: '2025-01-02T00:00:00Z',
    token: 'user2@example.com',
    version: 'user2@example.com',
    following: [
      'test@example.com',
      'user1@example.com',
      'user4@example.com'
    ],
    followers: [
      'test@example.com',
      'user1@example.com',
      'user3@example.com'
    ]
  },
  {
    email: 'user3@example.com',
    username: 'user3',
    password: '123123',
    avatar: avatar_female,
    useDefaultAvatar: true,
    createdAt: '2025-01-02T00:00:00Z',
    token: 'user3@example.com',
    version: 'user3@example.com',
    following: [
      'test@example.com',
      'user2@example.com',
      'user4@example.com'
    ],
    followers: [
      'user1@example.com',
      'user4@example.com'
    ]
  },
  {
    email: 'user4@example.com',
    username: 'user4',
    password: '123123',
    avatar: avatar_male,
    useDefaultAvatar: true,
    createdAt: '2025-01-02T00:00:00Z',
    token: 'user4@example.com',
    version: 'user4@example.com',
    following: [
      'user2@example.com',
      'user3@example.com'
    ],
    followers: [
      'test@example.com',
      'user1@example.com',
      'user2@example.com',
      'user3@example.com'
    ]
  }
];

// 生成已通过题目列表
const generateSolvedProblems = (userId, count = 5) => {
  const allProblems = generateProblems();
  const solved = allProblems
    .filter(p => Math.random() > 0.7) // 30%概率为已通过
    .slice(0, count)
    .map(p => p.getId().toString());
  return solved;
};

// 生成正在尝试题目列表
const generateAttemptingProblems = (userId, count = 5) => {
  const allProblems = generateProblems();
  const attempting = allProblems
    .filter(p => !p.getPassed()) // 未通过的题目
    .slice(0, count);
  return attempting;
};

// 用户统计数据
export const userStats = [
  {
    email: 'test@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com'],
    followers: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com'],
    mutualFriends: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com']
  },
  {
    email: 'user1@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user2@example.com', 'user3@example.com', 'user4@example.com'],
    followers: ['user2@example.com', 'user3@example.com', 'user4@example.com'],
    mutualFriends: ['user2@example.com', 'user3@example.com', 'user4@example.com']
  },
  {
    email: 'user2@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user1@example.com', 'user3@example.com', 'user4@example.com'],
    followers: ['user1@example.com', 'user3@example.com', 'user4@example.com'],
    mutualFriends: ['user1@example.com', 'user3@example.com', 'user4@example.com']
  },
  {
    email: 'user3@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user1@example.com', 'user2@example.com', 'user4@example.com'],
    followers: ['user1@example.com', 'user2@example.com', 'user4@example.com'],
    mutualFriends: ['user1@example.com', 'user2@example.com', 'user4@example.com']
  },
  {
    email: 'user4@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user1@example.com', 'user2@example.com', 'user3@example.com'],
    followers: ['user1@example.com', 'user2@example.com', 'user3@example.com'],
    mutualFriends: ['user1@example.com', 'user2@example.com', 'user3@example.com']
  },
  {
    email: 'user5@example.com',
    solvedCount: 11,
    totalProblems: 13,
    solvedProblems: ['1', '2', '3','4', '5', '6','7', '8', '9', '10', '11'],
    attemptingProblems: ['12', '13'],
    publishedBlogs: [1,2,3,4,5,6,7,8,9,10,11,12,13],
    draftBlogs: [21,22,23,24,25,26,27,28,29,30,31,32],
    following: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com'],
    followers: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com'],
    mutualFriends: ['user1@example.com', 'user2@example.com', 'user3@example.com', 'user4@example.com']
  }
];

// 获取用户题目列表(统一处理已通过和尝试中题目)
export const getUserProblems = (userId, type = true, page = 1, pageSize = 10) => {
  const userStat = userStats.find(u => u.email === userId);
  if (!userStat) return { 
    problems: [], 
    page,
    pageSize,
    totalPages: 0
  };

  const allProblems = generateProblems();
  let problemIds = type
    ? userStat.solvedProblems 
    : userStat.attemptingProblems;
  
  const total = problemIds.length;
  const totalPages = Math.ceil(total / pageSize);
  const problems = allProblems
    .filter(p => problemIds.includes(p.getId().toString()))
    .slice((page - 1) * pageSize, page * pageSize);

  return {
    problems,
    page,
    pageSize,
    totalPages
  };
};

// 更新用户名
export const updateUsername = async (email, newUsername) => {
  const user = users.find(u => u.email === email);
  if (!user) return { success: false, message: '用户不存在' };
  
  user.username = newUsername;
  return { success: true, message: '用户名更新成功' };
};

// 更新头像
export const updateAvatar = async (email, avatarData) => {
  const user = users.find(u => u.email === email);
  if (!user) return { success: false, message: '用户不存在' };
  
  user.avatar = avatarData;
  return { 
    success: true, 
    message: '头像更新成功',
    avatar: avatarData 
  };
};

// 修改密码
export const changePassword = async (email, currentPassword, newPassword) => {
  const user = users.find(u => u.email === email);
  if (!user) return { success: false, message: '用户不存在' };
  
  if (user.password !== currentPassword) {
    return { success: false, message: '当前密码不正确' };
  }
  
  user.password = newPassword;
  return { success: true, message: '密码修改成功' };
};

// 通过邮箱验证码修改密码
export const changePasswordByEmail = async (email, verificationCode, newPassword) => {
  const user = users.find(u => u.email === email);
  if (!user) return { success: false, message: '用户不存在' };
  
  if (verificationCodes[email] !== verificationCode) {
    return { success: false, message: '验证码不正确' };
  }
  
  user.password = newPassword;
  return { success: true, message: '密码修改成功' };
};

// 发送验证码
export const sendVerificationCode = async (email) => {
  if (!users.some(u => u.email === email)) {
    return false;
  }
  
  // 生成6位随机验证码
  const code = Math.floor(100000 + Math.random() * 900000).toString();
  verificationCodes[email] = code;
  
  console.log(`验证码已发送至 ${email}: ${code}`); // 开发环境下打印验证码
  return true;
};
